package com.cx.purchase.plan.controller;

import com.cx.purchase.config.ResultVO;
import com.cx.purchase.config.ResultVOBuilder;
import com.cx.purchase.plan.mapper.PlanclassMapper;
import com.cx.purchase.plan.model.*;
import com.cx.purchase.plan.model.*;
import com.cx.purchase.plan.service.PlanService;
import com.cx.purchase.plan.service.PlanServiceImpl;
import com.cx.purchase.util.AllrecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;

@RestController
@CrossOrigin //支持跨域请求
@Api(value = "PlanController" ,description = "计划大类功能")
public class PlanController {
    @Autowired
    private PlanclassMapper planclassMapper;

    @Autowired
    private PlanServiceImpl purchaseServiceImpl;

    @Autowired
    private PlanService planService;



    @PostMapping("/metadata/getPullDownList")
    public ResultVO getPullDownList(){
        ArrayList<StatusVo> list = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel("启用");
        statusVo.setValue(1);
        StatusVo statusVo1 = new StatusVo();
        statusVo1.setValue(0);
        statusVo1.setLabel("禁用");
        list.add(statusVo);
        list.add(statusVo1);

        return ResultVOBuilder.success(list);
    }

    @PostMapping("/planCategory/queryPage")
    @ApiOperation(value = "分页查询")
    public ResultVO<AllrecordsVo<PlanClass>> queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){
        //接受到了前端穿过来的参数
        //计算索引

        return purchaseServiceImpl.queryPage(planClassSelectVo);

    }

    @PostMapping("/planCategory/add")
    public ResultVO add(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        return purchaseServiceImpl.add(planClassAddVo);
    }

    @PostMapping("/planCategory/update")
    public ResultVO update(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bd){
        if(bd.hasErrors()){

            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        return  purchaseServiceImpl.update(planClassUpdateVo);
    }
    @PostMapping("/planCategory/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid PlanClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bd){
        if(bd.hasErrors()){

            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        return  purchaseServiceImpl.updateStatus(planClassUpdateStatusVo);
    }


    @GetMapping("/planClass/queryById")
    public ResultVO queryById( Integer id ){
        if(id==null){
            ResultVOBuilder.error("Id不能为空");
        }
        return   purchaseServiceImpl.queryById(id);
    }

    @GetMapping("/subclass/delete")
    public String delete(Integer id){
        int i = planclassMapper.delete(id);
        return  i > 0 ?"删除成功":"删除失败";
    }



    /**
     * 计划大类导入
     *1.给用户提供导入模板
     *
     2.提示版本问题，询问产品经理支持哪种版本的导入格式，2007版本的导入模板，根据版本不同，poi导入技术的读取方式不同
     3.模板是否是需要导入的模板，验证模板是否正确
     4.格式问题，必须保持统一格式，统一为字符串格式导入
     5.验证不为空字段是否为空
     6.验证字符长度是否符合需求
     7.验证数据是否存在也就是是否合法
     8.验证数据是否重复
     9.对于需要转换的数据进行数据转换，比如数据库存的数据为数字格式，用户输入的为汉字，需要把汉字转换为数字存储
     */
    @PostMapping("quotation/InquiryIntroduction")
    public ResultVO importFile(MultipartFile file)throws Exception{
        System.out.println(file.getOriginalFilename());
        if (!file.getOriginalFilename().contains("xlsx")){
            return ResultVOBuilder.error("请检查导入模板，尾缀名必须为xlsx");
        }

        ResultVO resultVO = planService.importFile(file);

        return ResultVOBuilder.success(resultVO);

    }

    /*
    * 计划大类导出
    * */
    @GetMapping("material/exportMater")
    public ResultVO export(String code, String name, Boolean status, HttpServletResponse response)throws Exception{
        PlanClassSelectVo planClassSelectVo = new PlanClassSelectVo();

        planClassSelectVo.setCode(code);
        planClassSelectVo.setName(name);
        planClassSelectVo.setStatus(status);
        return planService.exportFile(planClassSelectVo,response);
    }


}
